#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct Contact {
    char name[50];
    char phone[20];
    char address[100];
    struct Contact *next;
} Contact;

void enter(Contact **head);
void del(Contact **head);
void list(Contact *head);
void search(Contact *head);
void save(Contact *head);
void load(Contact **head);

int main() {
    Contact *head = NULL;
    int choice;

    while (1) {
        printf("\n通讯录管理系统\n");
        printf("1. 录入\n");
        printf("2. 删除\n");
        printf("3. 显示\n");
        printf("4. 查找\n");
        printf("5. 保存\n");
        printf("6. 加载\n");
        printf("0. 退出\n");
        printf("请输入选项：");
        scanf("%d", &choice);
        while (getchar() != '\n'); // 清空输入缓冲区

        switch (choice) {
            case 1:
                enter(&head);
                break;
            case 2:
                del(&head);
                break;
            case 3:
                list(head);
                break;
            case 4:
                search(head);
                break;
            case 5:
                save(head);
                break;
            case 6:
                load(&head);
                break;
            case 0: {
                // 释放所有内存
                Contact *current = head;
                while (current != NULL) {
                    Contact *temp = current;
                    current = current->next;
                    free(temp);
                }
                printf("系统已退出\n");
                exit(0);
            }
            default:
                printf("无效选项，请重新输入。\n");
        }
    }
    return 0;
}

void enter(Contact **head) {
    Contact *newContact = (Contact *)malloc(sizeof(Contact));
    if (newContact == NULL) {
        printf("内存分配失败！\n");
        return;
    }

    printf("请输入姓名：");
    fgets(newContact->name, sizeof(newContact->name), stdin);
    newContact->name[strcspn(newContact->name, "\n")] = '\0';

    printf("请输入电话：");
    fgets(newContact->phone, sizeof(newContact->phone), stdin);
    newContact->phone[strcspn(newContact->phone, "\n")] = '\0';

    printf("请输入地址：");
    fgets(newContact->address, sizeof(newContact->address), stdin);
    newContact->address[strcspn(newContact->address, "\n")] = '\0';

    newContact->next = NULL;

    if (*head == NULL) {
        *head = newContact;
    } else {
        Contact *current = *head;
        while (current->next != NULL) {
            current = current->next;
        }
        current->next = newContact;
    }
    printf("添加成功！\n");
}

void del(Contact **head) {
    if (*head == NULL) {
        printf("通讯录为空，无法删除。\n");
        return;
    }

    char name[50];
    printf("请输入要删除的姓名：");
    fgets(name, sizeof(name), stdin);
    name[strcspn(name, "\n")] = '\0';

    Contact *current = *head;
    Contact *prev = NULL;
    int found = 0;

    while (current != NULL) {
        if (strcmp(current->name, name) == 0) {
            if (prev == NULL) {
                *head = current->next;
            } else {
                prev->next = current->next;
            }
            free(current);
            printf("删除成功。\n");
            found = 1;
            break;
        }
        prev = current;
        current = current->next;
    }

    if (!found) {
        printf("未找到该联系人。\n");
    }
}

void list(Contact *head) {
    if (head == NULL) {
        printf("通讯录为空。\n");
        return;
    }

    Contact *current = head;
    int count = 1;
    while (current != NULL) {
        printf("\n联系人%d:\n", count++);
        printf("姓名：%s\n", current->name);
        printf("电话：%s\n", current->phone);
        printf("地址：%s\n", current->address);
        current = current->next;
    }
    printf("\n");
}

void search(Contact *head) {
    char name[50];
    printf("请输入要查找的姓名：");
    fgets(name, sizeof(name), stdin);
    name[strcspn(name, "\n")] = '\0';

    Contact *current = head;
    int found = 0;

    while (current != NULL) {
        if (strcmp(current->name, name) == 0) {
            printf("\n找到联系人：\n");
            printf("姓名：%s\n", current->name);
            printf("电话：%s\n", current->phone);
            printf("地址：%s\n\n", current->address);
            found = 1;
            break;
        }
        current = current->next;
    }

    if (!found) {
        printf("未找到该联系人。\n");
    }
}

void save(Contact *head) {
    FILE *fp = fopen("contacts.txt", "w");
    if (fp == NULL) {
        printf("无法打开文件进行保存。\n");
        return;
    }

    Contact *current = head;
    while (current != NULL) {
        fprintf(fp, "%s,%s,%s\n", current->name, current->phone, current->address);
        current = current->next;
    }

    fclose(fp);
    printf("保存成功。\n");
}

void load(Contact **head) {
    // 释放原有链表
    Contact *current = *head;
    while (current != NULL) {
        Contact *temp = current;
        current = current->next;
        free(temp);
    }
    *head = NULL;

    FILE *fp = fopen("contacts.txt", "r");
    if (fp == NULL) {
        printf("文件不存在或无法打开。\n");
        return;
    }

    char line[200];
    while (fgets(line, sizeof(line), fp)) {
        line[strcspn(line, "\n")] = '\0'; // 去除换行符

        Contact *newContact = (Contact *)malloc(sizeof(Contact));
        if (newContact == NULL) {
            printf("内存分配失败！\n");
            break;
        }

        char *token = strtok(line, ",");
        if (token) strcpy(newContact->name, token);
        token = strtok(NULL, ",");
        if (token) strcpy(newContact->phone, token);
        token = strtok(NULL, ",");
        if (token) strcpy(newContact->address, token);

        newContact->next = NULL;

        if (*head == NULL) {
            *head = newContact;
        } else {
            Contact *tail = *head;
            while (tail->next != NULL) {
                tail = tail->next;
            }
            tail->next = newContact;
        }
    }

    fclose(fp);
    printf("加载成功。\n");
}